package com.lagou.java.controller;

import com.lagou.java.dao.ResumeDao;
import com.lagou.java.pojo.Resume;
import com.lagou.java.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class DemoController {

    @Autowired
    private ResumeDao resumeDao;

    /**
     * 产品列表
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String name, Model model) {
        List<Resume> all = resumeDao.findAll();
        model.addAttribute( "listData", all);
        return "list";
    }

    /**
     * 登录验证成功跳转
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    public String login (HttpServletRequest request, HttpServletResponse response) throws Exception{
        return "redirect:list";
    }

    /**
     * 添加用户
     * @param resume
     * @param model
     * @return
     */
    @PostMapping(value = "/addUser", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String add(Resume resume, Model model) {
        Resume save = resumeDao.save(resume);
        return save != null ? "添加成功" : "添加失败";
    }

    /**
     * 查找用户信息
     * @param id
     * @return
     */
    @PostMapping(value = "/getData")
    @ResponseBody
    public Resume get(String id) {
        Resume one = resumeDao.findOne(Long.valueOf(id));
        return one;
    }

    /**
     * 更新用户信息
     * @param resume
     * @param model
     * @return
     */
    @PostMapping(value = "/updateData", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String update(Resume resume, Model model) {
        Resume save = resumeDao.save(resume);
        return save != null ? "修改成功" : "修改失败";
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @PostMapping(value = "/{id}/delete", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String delete(@PathVariable("id") String id) {
        resumeDao.delete(Long.valueOf(id));
        return "删除成功";
    }

}
